package com.easygoods.tasks;

import com.bestvike.stone.spring.page.PageableRequest;
import com.easygoods.dao.OrderInfoDao;
import com.easygoods.service.OrderTraceService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by 许崇雷 on 2019-01-18.
 */
@CommonsLog
@Component
public class OrderTraceScheduling {
    private static final int PAGE_SIZE = 50;

    @Autowired
    private OrderInfoDao orderInfoDao;
    @Autowired
    private OrderTraceService orderTraceService;

    //每天 0 点执行  秒 分 时 日 月 周
    @Scheduled(cron = "0 0 0 * * ?")
    public void queryTrace() {
        log.info("=======开始批量获取物流信息");
        PageableRequest request = new PageableRequest();
        request.setPageSize(PAGE_SIZE);
        request.setPageNum(1);
        List<String> traceOrderIdList = this.orderInfoDao.getUnderWayIdList(request.toRowBounds());
        while (traceOrderIdList.size() > 0) {
            for (String orderId : traceOrderIdList) {
                try {
                    this.orderTraceService.getOrderTraces(orderId);
                    log.info("=======获取物流信息完成 orderId:" + orderId);
                } catch (Exception e) {
                    log.error("获取物流信息失败 orderId:" + orderId, e);
                }
            }
            request.setPageNum(request.getPageNum() + 1);
            traceOrderIdList = this.orderInfoDao.getUnderWayIdList(request.toRowBounds());
        }
        log.info("=======批量获取物流信息完成");
    }
}
